<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>纯青管理系统</title>
    <include file="Public:header" />
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerGrid.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerToolBar.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerTab.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerDrag.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerTree.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerLayout.js" type="text/javascript"></script>
     <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>

    
    <script type="text/javascript">
    var tab,selectedRoleID,selectedRoleName ;
    var rd={$roleTreeData};
        $(function ()
        {
        	$("#layout1").ligerLayout({ leftWidth: 150});
            $("#tree1").ligerTree({
            	checkbox: false,
            	 attribute: ['nodename', 'id'],
                data:rd,
                onSelect: function (node)
                {
                	if (!node.data.id) return;
                	selectedRoleID=node.data.id; 
                	selectedRoleName=node.data.text;
                	checkRight(selectedRoleID);
                }
            });


        	$("#navtab1").ligerTab(); 
        	tab = $("#navtab1").ligerGetTabManager();
        	

            //权限 保存按钮
            var toolbarOptions = {
                items: [
                { text: '保存', click: f_save, img: "__PUBLIC__/lib/icons/silkicons/page_save.png" }
            ]
            };
            //权限
            var gridRight = $("#rightgrid").ligerGrid({
                columns:
                    [
                    { display: '分配权限' , name:'Permit',align: 'left', width: 60, minWidth: 60,isAllowHide :false, render: function (rowdata)
                    {
                        var iconHtml = '<div class="access-icon access-permit';
                        if (rowdata.Permit) iconHtml += " access-icon-selected";
                        iconHtml += '"';
                        iconHtml += ' rowid = "' + rowdata['__id'] + '"';
                        iconHtml += '></div>';
                        return iconHtml;
                    }
                    },
                    { display: 'id', name: 'id', align: 'left', width: 40, minWidth: 20 },
                    { display: '应用-模块-操作', name: 'title', align: 'left', width: 200, minWidth: 60 },
                    { display: '名称', name: 'name', align: 'left', width: 100, minWidth: 60 },
                    { display: '图标', name: 'nodeicon', align: 'left', width: 100, minWidth: 60,render: function (rowdata, rowindex, value){if(value){return '<IMG SRC="__ROOT__/'+value+'" WIDTH="20" HEIGHT="20" BORDER="0"/>';}}}
                    ],
                showToggleColBtn: false, width: '99%',height:'98%', rowHeight: 20, fixedCellHeight: true,
                columnWidth: 100, frozen: false, usePager: false, checkbox: false, rownumbers: true,
                toolbar: toolbarOptions, tree: { columnName: 'title' }, 
                url: "__URL__/getNodeData/"
            });

          //分配权限、禁止权限按钮的事件
            $("div.access-icon").live('click', function ()
            {
                var selected = !$(this).hasClass("access-icon-selected");
                var ispermit = $(this).hasClass("access-permit");
                var rowid = $(this).attr("rowid");
                var rowdata = gridRight.records[rowid];
                    f_Permit(rowdata, selected); 
             
            });

            //为当前选择记录 分配权限
            //1,同时分配给下级记录 
            function f_Permit(rowdata, selected)
            {
               
            	selected = selected ? true : false;
                rowdata.Permit = rowdata.Permit ? true : false;
                if (rowdata.Permit == selected) return;
                rowdata.Permit = selected;
               
               var children = gridRight.getParent(rowdata);

                if (children)
                {
                	if(selected)f_Permit(children, selected);
                 /*  for (var i = 0, l = children.length; i <=l; i++)
                    {
                    	alert(children);
                        f_Permit(children[i], selected);
                    }*/
                }
                gridRight.reRender({ rowdata: rowdata });
            }

            function checkRight(groupId){
            	$.post("__URL__/getRight/",{"groupId":groupId}, function(data){
               	   var rows = gridRight.rows;
               	 //  alert((data[0].id));
                    for (var i = 0, l = rows.length; i < l; i++)
                    {
                        rows[i].Permit = checkPermit(rows[i],jQuery.parseJSON(data));
                    }
                    gridRight.reRender();
                });
            }
            //判断是否有权限
            function checkPermit(rowdata, data)
            {
               
            	if (!data || !data.length) return false;
                for (var i = 0, l = data.length; i < l; i++)
                {
                	if(data[i].id==rowdata.id)return true;
                }
                return false;
            }
            

            function f_save()
            {
                if (!selectedRoleID){alert('未选择角色!'); return;}
                var data = [];
                for (var i = 0, l = gridRight.rows.length; i < l; i++)
                {
                    if(gridRight.rows[i].Permit)data.push(gridRight.rows[i].id);
                      
                }
             //   alert(JSON2.stringify(data));
               
                $.post("__URL__/setRight/",{"accessList":data, "roleId": selectedRoleID,"roleName":selectedRoleName}, function(data){
                	LG.tip(data);
                 });

            }
            
        });
    </script>
    <style type="text/css">
    .cell-label{ width:80px;}
    #tabcontainer .l-tab-links{border-top:1px solid #D0D0D0;border-left:1px solid #D0D0D0;border-right:1px solid #D0D0D0;}
    
    .projectgrid .l-selected .l-grid-row-cell,.projectgrid .l-selected {background: none;}
    
    .access-icon{ background:url(__PUBLIC__/lib/ligerUI/skins/Aqua/images/controls/checkbox.gif) 0px 0px; height:13px; line-height:13px; width:13px; margin:4px 20px; display:block; cursor:pointer;}
    .access-icon-selected{ background-position:0px -13px;} 
    .l-panel td.l-grid-row-cell-editing { padding-bottom: 2px;padding-top: 2px;}
    </style>
</head>
<body style="padding:10px">
<div id="layout1">

            <div position="left" title="角色">
              <div style="margin:10px; float:left; border:1px solid #ccc; overflow:auto;  ">
			   <ul id="tree1"></ul>
		      </div> 
	          <div style="display:none"></div>
            </div>

            <div position="center" title="权限控制" >
            	<div id="rightgrid" style="margin:2px auto;"></div>
			</div>  
 </div> 

	
</body>
</html>

